<template>
  <el-dialog
    title="流程表单"
    :visible="visible"
    :append-to-body="true"
    @close="handleDialogClose"
    width="600px"
  >
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="流程表单">
        <el-select v-model="form.formId" class="form-item" placeholder="请选择流程表单">
          <el-option
            v-for="item of formList"
            :key="item.formId"
            :label="item.formName"
            :value="item.formId"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import FlowInfoService from '../../../../service/flow_info'
import FormDesignService from '../../../../service/form_design'

export default {
  props: {
    show: {
      type: Boolean,
      required: true,
      default: () => false
    },
    data: {
      type: Object,
      required: false,
      default: () => {}
    }
  },
  data() {
    return {
      visible: false,
      formList: [],
      form: {
        formId: ''
      }
    }
  },
  mounted() {
    this.getFormList()
  },
  methods: {
    // 处理Dialog关闭事件
    handleDialogClose() {
      this.$emit('update:show')
    },
    // 获取表单列表
    getFormList() {
      FormDesignService.getFormListAll().then(res => {
        this.formList = res.data
      })
    },
    // 提交表单
    submitForm() {
      const flow = {
        flowKey: this.data.key,
        formId: this.form.formId
      }

      FlowInfoService.updateFlowInfo(flow).then(res => {
        this.$message.success('设置成功')
        this.visible = false
        this.$emit('success')
      })
    }
  },
  watch: {
    show(value) {
      if (value) {
        this.form.formId =
          (this.data.flowInfo && this.data.flowInfo.formId) || ''
      }
      this.visible = value
    }
  }
}
</script>

<style lang="stylus" scoped>
.form-item
  width 100%
</style>
